import { Component, OnInit } from '@angular/core';
import { Router ,RouteParams} from '@angular/router-deprecated';
import { BaseRequestOptions,Http,Response, HTTP_PROVIDERS,Headers,URLSearchParams} from '@angular/http';
import {ProjService} from '../services/proj.service';
import {GlobalService} from '../services/global.service';
import {Proj} from '../model/proj.model';

@Component({
  selector: 'proj-component',
  viewProviders: [HTTP_PROVIDERS,ProjService],
  templateUrl: 'app/proj/tpl/proj.component.html'
})
export class ProjComponent implements OnInit {

  proj:Proj= new Proj();
  constructor(private router:Router,params:RouteParams,private projService:ProjService,private globalService:GlobalService) {
    projService.authInit(router,()=>{
      this.proj._id = params.get('id');
      if(this.proj._id !== undefined && this.proj._id !== "" && this.proj._id!== null)
      {
        var p = this.projService.getProj(this.proj._id);
        p.subscribe(data=> this.proj = data.json());
      }
    });
  }
  ngOnInit() {

  }

  setOutgiving(b)
  {
    this.proj.outgiving = b;
  }

  submit()
  {
    if(this.proj._id === undefined || this.proj._id === "" ||  this.proj._id===null)
    {
      this.proj.ownerId = "kinglo";
      this.projService.addProj(this.proj).subscribe(data=>this.router.navigate(['ShowProjs']));
    }else
    {
      this.projService.updateProj(this.proj).subscribe(data=>this.router.navigate(['ShowProjs']));
    }
  }
  delete()
  {
    $('.ui.basic.modal')['modal']('show');
  }

  cancelDelete()
  {
    $('.ui.basic.modal')['modal']('hide');
  }

  confirmDelete()
  {
    this.projService.deleteProj(this.proj._id).subscribe(()=>{
      this.cancelDelete();
      this.router.navigate(["ShowProjs"]);
    });
  }

}

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
